#include "TelephoneBook.h"
#include "Contacts.h"

#include <stdio.h>
#include <stdbool.h>
#include <string.h>
#include <stdlib.h>

struct TelephoneBook
{
    ElementType *array; // 数组指针
    int capacity;       // 数组容量
    int size;           // 数组大小
};

void Interface(Telebook *t)
{
    printf("1.添加联系人\n");
    printf("2.删除联系人\n");
    printf("3.循环查询\n");
    printf("4.修改联系人\n");
    printf("5.打印联系人信息\n");
    printf("6.模糊查询\n");
    printf("7.快速查找\n");
    printf("8.退出\n");
}



Telebook *InitTelebook()
{
    Telebook *t = (Telebook *)malloc(sizeof(Telebook));
    if (t == NULL)
    {
        printf("InitTelebook malloc error!\n");
        exit(-1);
    }
    t->size = 0;
    t->capacity = 10;
    t->array = (ElementType *)malloc(sizeof(ElementType) * t->capacity);
    if (t->array == NULL)
    {
        printf("InitTelebook array error!\n");
        exit(-1);
    }
    return t; // 返回结构体指针
}

void InsertSort(Telebook *t,int (*compare)(ElementType, ElementType))
{
    for(int i = 1;i < t->size;i++)
    {
        int j = i;//即将要插入的数
        int *temp = t->array[j];
        for(;j > 0;j--)
        {
            if(compare(t->array[j-1],temp) > 0)//前面的数比我大
            {
                t->array[j] = t->array[j-1];
            }
            else//前面数比我大
            {
                break;
            }
        }
        t->array[j] = temp;
    }
}


void GetTxt(Telebook *t)
{
    FILE* f = fopen("./通讯录.txt","r");
    if(f == NULL)
    {
        printf("文件打开失败\n");
        return ;
    }
    
    fseek(f,0,SEEK_END);
    long len = ftell(f);
    printf("%ld\n",len);
    fseek(f,0,SEEK_SET);
    long size = ftell(f);
    while(size != len)
    {
        t->array[t->size++] = GetConts(f);
        size = ftell(f);
    }
    fclose(f);
    
}


void InsertTelebook(Telebook *t)
{
    if (t->size == t->capacity)
    {
        t->capacity *= 2; // 可以扩两倍
        t->array = (ElementType *)realloc(t->array, t->capacity * sizeof(ElementType));
        if (t->array == NULL)
        {
            printf("InitTelebook realloc error!\n");
            exit(-1);
        }
    }
    
    t->array[t->size++] = CreateConts();
    if(t->size > 1)
        InsertSort(t,Namecmp);
        

}

ElementType RemoveByIndex(Telebook *t, int index)
{
    ElementType result = t->array[index];
    for (int i = index; i < t->size - 1; i++)
    {
        t->array[i] = t->array[i + 1];
    }
   // ClearConts(t->array[t->size]);
    t->size--;
    return result;
}

ElementType RemoveTelebook(Telebook *t, bool (*compare)(ElementType, ElementType))
{
    printf("请输入要移除的用户名字/电话号码: \n");
    getchar();
    char value[15];
    scanf("%s", value);
    for (int i = 0; i < t->size; i++)
    {
        if (compare(t->array[i], value))
        {
           return RemoveByIndex(t, i);
        }
    }
    return NULL;
}

int BinarySearch(Telebook *t,ElementType element,int (*compare)(ElementType, ElementType))
{
    int left = 0;
    int right = t->size -1;
    while(left <= right)
    {
        int mid = left + (right - left)/2;
        if(compare(t->array[mid],element) == 0)
            return mid;
        else if(compare(t->array[mid],element) > 0)
            right = mid - 1;
        else if(compare(t->array[mid],element) < 0)
            left = mid + 1;
    }
    return -1;
}

void BirnayFind(Telebook *t, int (*compare)(ElementType, ElementType))
{
    printf("请输入要查找的用户名字: \n");
    char value[15];
    scanf("%s", value);
    int mid = BinarySearch(t,value,Namecmp2);
    if(mid >= 0)
        PrintConts(t->array[mid]);
    else
        printf("查无此人！\n");
    getchar();
    getchar();
}

void FindByElement(Telebook *t, bool (*compare)(ElementType, ElementType))
{
    printf("请输入要查找的用户名字: \n");
    char value[15];
    int flag = 0;
    scanf("%s", value);
    for (int i = 0; i < t->size; i++)
    {
        if (compare(t->array[i], value))
        {
            PrintConts(t->array[i]);
            flag = 1;
        }
    }
    if(flag == 0)
        printf("查无此人！\n");
    getchar();
    getchar();
}

void ModifyByElement(Telebook *t, bool (*compare)(ElementType, ElementType))
{
    printf("请输入要修改的用户名字/电话号码: \n");
    char value[15];
    scanf("%s", value);
    for (int i = 0; i < t->size; i++)
    {
        if (compare(t->array[i], value))
        {
            ClearConts(t->array[i]);
            t->array[i] = CreateConts();
        }
    }
}

void OrderPrint(Telebook *t, void (*print)(ElementType))
{
        printf("联系人信息: \n");
    for (int i = 0; i < t->size; i++)
    {
        print(t->array[i]);
    }
}

void ReversePrint(Telebook *t, void (*print)(ElementType))
{
        printf("联系人信息: \n");
    for (int i = t->size - 1; i >= 0; i--)
    {
        print(t->array[i]);
    }
}


void PrintTelebook(Telebook *t, void (*print)(ElementType))
{
    InsertSort(t,Namecmp);
    printf("1.顺序显示  2.逆序显示\n");
    int n = 0;
    scanf("%d",&n);
    if(n == 1)
        OrderPrint(t,PrintConts);
    else if(n == 2)
        ReversePrint(t,PrintConts);
    else
        printf("输出错误序号，已退出！\n");

    
    // print("1.顺序显示  2.逆序显示");
    // int n = 0;
    // scanf("%d",&n);
    // switch(n)
    // {
    //     case 1:
    //         printf("联系人信息: \n");
    //         for (int i = 0; i < t->size; i++)
    //         {
    //             print(t->array[i]);
    //         }
    //         break;
    //     case 2:
    //         printf("联系人信息: \n");
    //         for (int i = t->size - 1; i >= 0; i--)
    //         {
    //             print(t->array[i]);
    //         }
    //         break;
    //     default:
    //         break;
    // }
    printf("\n");
    getchar();
    getchar();
}

void FuzzyFind(Telebook *t, bool (*compare)(ElementType, ElementType))
{
    printf("请输入要查找的用户名字: \n");
    char value[15];
    int flag = 0;
    scanf("%s", value);
    for (int i = 0; i < t->size; i++)
    {
        if (compare(t->array[i], value))
        {
            PrintConts(t->array[i]);
            flag = 1;
        }
    }
    if(flag == 0)
        printf("查无此人！\n");
    getchar();
    getchar();
}

void ClearTelebook(Telebook *t, void (*clear)(ElementType))
{
    if (t != NULL)
    {
        for (int i = 0; i < t->size; i++)
        {
            clear(t->array[i]);
        }
        free(t->array);
        free(t);
    }
}

void Save(Telebook *t)
{
    FILE* f = fopen("./通讯录.txt","w");
    if(f == NULL)
    {
        printf("文件打开失败\n");
        return ;
    }
    fclose(f);
    for(int i = 0;i < t->size;i++)
    {
        SaveConts(t->array[i]);
    }
}
